package com.qst.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
//将MyBatisConfig类和JdbcConfig类交给Spring来管理
@Import({MyBatisConfig.class, JdbcConfig.class})
// 开启包扫描等同于:<context:component-scan base-package="com....">
@ComponentScan("com.qst.service.impl")
// 开启事务
@EnableTransactionManagement
public class SpringConfig {
    /*
    等同于<bean class="org....DataSourceTransactionManager">
    */
    @Bean("transactionManager")
    public DataSourceTransactionManager getDataSourceTxManager(@Autowired DataSource dataSource){
        DataSourceTransactionManager dtm = new DataSourceTransactionManager();
        //等同于<property name="dataSource" ref="dataSource">
        dtm.setDataSource(dataSource);
        return dtm;
    }
}
